<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Logistics\Controllers;

use App\Eloquent\Ygt\Company;
use App\Eloquent\Ygt\Customer;
use App\Eloquent\Ygt\CustomerOrder;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\LogisticsNotes;
use App\Eloquent\Ygt\UserMessage;
use App\Eloquent\Ygt\Warehouse;
use App\Eloquent\Ygt\WarehouseSendRelation;
use Framework\BaseClass\Api\Controller;
use App\Engine\Func;

class MessageController extends Controller
{
    public function detail()
    {
        $userId             = Func::getHeaderValueByName('userid',25);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $messageId          = request('message_id',0);
        if(!$userInfo || !$messageId)
        {
            xThrow( ERR_PARAMETER );
        }
        $where              = ['id'=>$messageId];
        $message            = UserMessage::getInfo($where);
        if(!$message)
        {
            xThrow(ERR_UNKNOWN,' 消息出错');
        }
        $foreignKey         = $message->foreign_key;
        $type               = $message->type;
        $typeId             = $message->type_id;
        $where              = ['id'=>$typeId];
        $notes              = LogisticsNotes::getInfoData($where);
        if(!$notes)
        {
            xThrow(ERR_UNKNOWN,'物流信息出错');
        }
        switch ($type)
        {
            case 21:
                break;
            case 22:
                break;
            default:
                xThrow(ERR_UNKNOWN);
        }
        $result         = ['info'=>$notes];
        return $result;
    }
    //ui图界面,暂时不走
    public function detailXXXXXXXXXXXXXXXXXXXXXXX()
    {
        $userId             = Func::getHeaderValueByName('userid',25);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $messageId          = request('message_id',0);
        if(!$userInfo || !$messageId)
        {
            xThrow( ERR_PARAMETER );
        }
        $where              = ['id'=>$messageId];
        $message            = UserMessage::getInfo($where);
        if(!$message)
        {
            xThrow(ERR_UNKNOWN,' 消息出错');
        }
        $userCompanyId      = $userInfo->company_id;
        $foreignKey         = $message->foreign_key;
        $type               = $message->type;
        $typeId             = $message->type_id;
        $customerOrder      = CustomerOrder::where('id',$foreignKey)->first();
        if(!$customerOrder)
        {
            xThrow(ERR_UNKNOWN,'订单信息出错');
        }
        $where              = ['id'=>$typeId];
        $notes              = LogisticsNotes::getInfoData($where);
        if(!$notes)
        {
            xThrow(ERR_UNKNOWN,'物流信息出错');
        }
        $notesCompnayId     = $notes['company_id'];
        //客户或者厂的信息4283-厂 4282-客户
        //21发货的时候给客户发消息,22发货的时候给销售和下单人发消息
        $customer           = $company = [];
        switch ($type)
        {
            case 21:
                //客商登录时
                //需要展示厂的一些信息
                $where          = ['id'=>$notesCompnayId];
                $company        = Company::getInfo($where);
                if(!$company)
                {
                    xThrow(ERR_UNKNOWN,'厂信息出错');
                }
                break;
            case 22:
                //工厂角色登录
                //需要展示客户的一些信息
                $customerId     = intval($customerOrder->customer_name);
                $where          = ['id'=>$customerId];
                $customer       = Customer::getInfo($where);
                if(!$customer)
                {
                    xThrow(ERR_UNKNOWN,'客户信息出错');
                }
                break;
            default:
                xThrow(ERR_UNKNOWN);
        }
        //关联的发货单
        $warehouseSendId    = $notes['warehouse_send_id'];
        //发货单相关的详细信息
        $where              = ['warehouse_send_id'=>$warehouseSendId];
        $relationList       = WarehouseSendRelation::getList($where)->toArray();
        $warehouseIdArr     = $relationNumberArr = [];
        foreach($relationList as $key=>$val)
        {
            $warehouseIdArr[]       = $val['warehouse_id'];
            $relationNumberArr[$val['order_id']]    = $val['number'];
        }
        //发货单相关的详细信息中相关联的成品
        $where                  = ['id'=>['in',$warehouseIdArr]];
        $warehouseList          = Warehouse::getList($where)->toArray();
        $result             = [
            'customer'=>$customer,
            'company'=>$company,
            'info'=>$notes,
            'order_list'=>$warehouseList,
        ];
        return $result;
    }
}